package com.bdqn.zhang;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

import androidx.appcompat.app.AppCompatActivity;

import com.bdqn.zhang.pojo.NewsEntity;
import com.google.gson.Gson;

import java.util.List;
import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {


    private ListView listView;

    private Button btn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listView = findViewById(R.id.list);

        btn = findViewById(R.id.btn);

        btn.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                try  {
                    OkHttpClient client = new OkHttpClient.Builder()   //创建okhttp实例
                            .connectTimeout(5, TimeUnit.SECONDS)  //设置请求超时
                            .readTimeout(5,TimeUnit.SECONDS).build();
                    Request request = new Request.Builder()
                            .url("http://v.juhe.cn/toutiao/index?type=top&key=4fa319ecebf7396d6b5c75f51c84c197#")
                            .build();

                        Response response = client.newCall(request).execute(); //获取服务器返回的数据
                        String responseData = response.body().string();

                        Gson gson = new Gson();
                        NewsEntity newsEntity = gson.fromJson(responseData, NewsEntity.class);//result就是服务器返回的Json字符串

                        List<NewsEntity.ResultBean.DataBean> list = newsEntity.getResult().getData();

                        String[] data = null;
                        if(list != null && list.size() > 0){
                            data = new String[list.size()];
                            for (int i = 0; i < list.size(); i++) {
                                data[i] = list.get(i).getTitle();
                            }
                        }
                        Message message = Message.obtain();
                        message.obj = data;
                        handler.sendMessage(message);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }});
        thread.start();
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            String[] titles = (String[]) msg.obj;
            //Update UI
            if (titles != null && titles.length > 0) {//set Adapter
                listView.setAdapter(new ArrayAdapter(MainActivity.this, android.R.layout.simple_list_item_1, titles));
            }
        }
    };

}
